package com.sskj.level.presenter;

import android.text.TextUtils;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.util.SPUtil;
import com.sskj.level.bean.CoinUpdateBean;
import com.sskj.level.bean.TradeInfoBean;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.StockCoinBean;
import com.sskj.lib.bean.SysConfigBean;
import com.sskj.lib.bean.WSFiveBean;
import com.sskj.level.http.HttpConfig;
import com.sskj.level.ui.fragment.BuyAndSellFragment;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;
import com.sskj.lib.util.ExceptionUtil;

import java.math.BigDecimal;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.functions.BiFunction;


public class BuyAndSellFragmentPresenter extends BasePresenter<BuyAndSellFragment> {

    public void getPankou(String code) {
        httpService.getPankou(code)
                .execute(new JsonCallBack<HttpData<WSFiveBean>>() {

                    @Override
                    public void onSuccess(Response<HttpData<WSFiveBean>> response) {
                        HttpData<WSFiveBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateFive(response.body().getData());
                        }

                    }
                });
    }

    public void createOrder(String entrustLot, String buyPrice, String stockCode, String billPriceType,
                            String buyBillType, String stopProfit, String stopLoss, String lever) {
        httpService.createOrder(entrustLot, buyPrice, buyBillType, stockCode, billPriceType, stopProfit, stopLoss, lever)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());

                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.buySuccess();
                        }
                    }
                });
    }

    public void getCoinInfo(String code) {
        ((BaseActivity) (mView.getActivity())).showLoading();
        Flowable<StockCoinBean> coinFlow = httpService.getProduct(code)
                .tag(this)
                .converter(new JsonConverter<HttpData<List<StockCoinBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(listHttpData -> listHttpData.getData().get(0));
        Flowable<HttpData<List<SysConfigBean>>> sysFlow = httpService.getSysConf("5")
                .tag(this)
                .converter(new JsonConverter<HttpData<List<SysConfigBean>>>() {
                })
                .adapt(new FlowableBody<>());
        Flowable.zip(coinFlow, sysFlow, new BiFunction<StockCoinBean, HttpData<List<SysConfigBean>>, CoinUpdateBean>() {
            @Override
            public CoinUpdateBean apply(StockCoinBean stock, HttpData<List<SysConfigBean>> listHttpData) throws Exception {
                CoinUpdateBean coinUpdateBean = new CoinUpdateBean();
                Flowable.fromIterable(listHttpData.getData())
                        .filter(item -> item.getName().equals("lever_deal_fee"))
                        .subscribe(item -> coinUpdateBean.setShouPer(item.getValue()));
                Flowable.fromIterable(listHttpData.getData())
                        .filter(item -> item.getName().equals("level_deal_num"))
                        .subscribe(item -> coinUpdateBean.setLevels(item.getValue()));
                coinUpdateBean.setCode(stock.getCode());
                coinUpdateBean.setName(stock.getName());
                coinUpdateBean.setMinDealNum(stock.getContractMin() + "");
                coinUpdateBean.setPrice(stock.getStockProductVO().getPrice());
                coinUpdateBean.setSlidingScalePrice(stock.getSlidingScalePrice() + "");
                coinUpdateBean.setSpread(stock.getSpread() + "");
                coinUpdateBean.setStopLoss(stock.getStopLoss() + "");
                coinUpdateBean.setStopProfit(stock.getStopProfit() + "");
                coinUpdateBean.setMinStopLoss(stock.getMinStopLoss() + "");
                coinUpdateBean.setMinStopProfit(stock.getMinStopProfit() + "");
                return coinUpdateBean;
            }
        }).subscribe(stockLevel -> {
            if (mView != null && mView.getActivity() != null) {
                ((BaseActivity) (mView.getActivity())).hideLoading();
                mView.updateCoinData(stockLevel);
            }
        }, e -> {
            if (mView != null && mView.getActivity() != null) {
                ((BaseActivity) (mView.getActivity())).hideLoading();
            }
            ExceptionUtil.dealException(e);
        });
    }

    // 获取风险率
    public void getRisk() {
        if (TextUtils.isEmpty(SPUtil.get(SPConfig.ID, ""))) {
            mView.updateRisk(null);
            return;
        }
        httpService.getRisk()
                .tag(this)
                .execute(new JsonCallBack<HttpData<TradeInfoBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<TradeInfoBean>> response) {
                        HttpData<TradeInfoBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateRisk(httpData.getData());
                        }
                    }
                });
    }
}
